﻿using System;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

//CID 0638 //2010.08.07 //Begin
namespace InfoJetSoft.Service.Worker
{
    class XSNViewFormatHyperlinkBoxWorker
	{
		
		public static void  formatHyperlinkBox(View view)
		{
			string spanFilter = "//span[@" + NamespaceUtil.toFullName(view.XdPrefix, "xctname") + "='" + JetConstant.XCT_HyperlinkBox + "']";

            XmlNodeList spanElementList = DOMUtil.selectNodes(view.Doc.DocumentElement, spanFilter, 0, view.NsResolver);
            foreach (XmlElement spanElement in spanElementList)
			{
                XmlElement buttonElement = spanElement.SelectSingleNode( "button" ) as XmlElement;
                XmlElement aElement = spanElement.SelectSingleNode(".//A") as XmlElement;
                if (buttonElement != null)
                {
                    if (aElement != null) {
                        XmlElement parentWhenElement = aElement.ParentNode as XmlElement;
                        string test = parentWhenElement.GetAttribute( "test" );
                        if (test.Length > 0)
                        {
                            XmlElement spanOnClickElement = XSNViewFormatHyperlinkBoxWorker.createOnClickElementGroup(view, "not (" + test + ")");
                            spanElement.InsertBefore(spanOnClickElement, buttonElement);
                            XmlElement buttonOnClickElement = XSNViewFormatHyperlinkBoxWorker.createOnClickElementGroup(view, test);
                            buttonElement.InsertBefore(buttonOnClickElement, buttonElement.FirstChild);
                        }
                    }
                    XmlElement imgElement = buttonElement.SelectSingleNode("img") as XmlElement;
                    if (imgElement != null)
                    {
                        imgElement.SetAttribute("src", view.Manifest.ImageUri.HyperlinkBox);
                    }
                    string bindingSecondary = spanElement.GetAttribute("binding_secondary", JetConstant.URI_XD);
                    if (bindingSecondary != null && bindingSecondary.Length > 0)
                    {
                        //目前暂不支持SDS secondary binding，因为更新SDS需要postback。
                        spanElement.SetAttribute("binding_secondary_id", "{" + NamespaceUtil.toFullName(view.XdExtensionPrefix, "GetMainJetId(") + bindingSecondary + ")}");
                    }
                }
                if (aElement != null)
                {
                    aElement.SetAttribute( "target", "_blank" );
                }
			}
		}

        private static XmlElement createOnClickElementGroup(View view, string test)
        {
            XmlElement chooseElement = view.Doc.CreateElement(view.XslPrefix, "choose", JetConstant.URI_XSL);
            XmlElement whenElement = view.Doc.CreateElement(view.XslPrefix, "when", JetConstant.URI_XSL);
            chooseElement.AppendChild(whenElement);
            whenElement.SetAttribute("test", test);
            XmlElement attrElement = view.Doc.CreateElement(view.XslPrefix, "attribute", JetConstant.URI_XSL);
            whenElement.AppendChild(attrElement);
            attrElement.SetAttribute("name", "onclick");
            attrElement.InnerText = "return InfoJet_OnHyperlinkBoxClick(this);";
            return chooseElement;
        }
	}
}
//CID 0638 //2010.08.07 //End